package com.ali.exception;

import com.ali.response.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理器
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020/5/27
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler  {

    /**
     * 指定出现什么异常执行这个方法
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity error(Exception e) {
        e.printStackTrace();
        return ResponseEntity.error().message(e.getMessage());
    }

    /**
     * 特定异常
     */
    @ExceptionHandler(ArithmeticException.class)
    public ResponseEntity error(ArithmeticException e) {
        e.printStackTrace();
        return ResponseEntity.error().message("执行了ArithmeticException异常处理..");
    }

    /**
     * 自定义的NotFoundException异常
     */
    @ExceptionHandler(NotFoundException.class)
    public ResponseEntity error(NotFoundException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseEntity.error().code(e.getCode()).message(e.getMsg());
    }

    /**
     * 自定义的ServiceException异常
     */
    @ExceptionHandler(ServiceException.class)
    public ResponseEntity error(ServiceException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseEntity.error().code(e.getCode()).message(e.getMsg());
    }

    /**
     * 自定义的VerifyException异常
     */
    @ExceptionHandler(VerifyException.class)
    public ResponseEntity error(VerifyException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseEntity.error().code(e.getCode()).message(e.getMsg());
    }

}
